﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for QueryStringHelper
/// </summary>
public class QueryStringHelper
{
	static QueryStringHelper()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static string GetRootURI(HttpContext context)
    {

        string UrlAuthority = context.Request.Url.GetLeftPart(UriPartial.Authority);
        if (context.Request.ApplicationPath == null || context.Request.ApplicationPath == "/")
        {
            //直接安装在Web站点
            return UrlAuthority;
        }
        else
        {
            //安装在虚拟子目录下
            return UrlAuthority + context.Request.ApplicationPath;
        }
    }


    /// <summary>
    /// 生成查询字符串
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public static String BuilderQueryString(System.Collections.Specialized.NameValueCollection list)
    {
        String str = null;
        foreach (String key in list.Keys)
        {
            //if (!key.Equals(CommonString.REQUESTKEY))
            {
                str += String.Format("{0}={1}&", key, list[key]);
            }
        }
        str = str.Substring(0, str.Length - 1);
        return str;
    }

    /// <summary>
    /// 生成查询字符串
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public static System.Collections.Specialized.NameValueCollection DecodeQueryString(System.Collections.Specialized.NameValueCollection list)
    {
        System.Collections.Specialized.NameValueCollection newlist = new System.Collections.Specialized.NameValueCollection();
        foreach (String key in list.Keys)
        {
            if (!key.ToUpper().Equals(CommonString.REQUESTKEY))
            {

                newlist.Add(key, list[key]);
            }
        }
        return newlist;
    }

    /// <summary>
    /// 生成请求KEY
    /// </summary>
    /// <param name="list"></param>
    /// <param name="Version"></param>
    /// <returns></returns>
    public static String BuilderRequestKey(System.Collections.Specialized.NameValueCollection list, String Version)
    {
        String str = null;
        foreach (String key in list.Keys)
        {
            if (!key.ToUpper().Equals(CommonString.REQUESTKEY) && !key.ToUpper().Equals(CommonString.VERSION))
            {
                str += list[key];
            }
        }
        return Jumpton.Common.Functions.EncryptString(str + Keys.SecurityKey[Version]);
    }
}